<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/25
 * Time: 16:59
 */
namespace app\common\model;

use think\Model;

class Setting extends Model
{
    protected $createTime = 'add_time';

    /**
     * 取得设置信息
     * @param String $field
     * @return string
     */
    public static function get_setting_info($field, $open_card_id = 0,  $company_id = 0)
    {
        if (!$open_card_id) {
            $open_card_id = OpenCard::get_open_card_id();
        }
        if (!$company_id) {
            $company_id = Company::get_company_id();
        }

        $setting_value = _uri('setting', array('company_id'=>$company_id, 'open_card_id'=>$open_card_id, 'field'=>$field), 'value');

        return $setting_value['value'];
    }


    /**
     * 获取总站host
     * @return string host
     */
    public static function get_domain()
    {
        /* if (ONDEV) {
            if (strpos($_SERVER['HTTP_HOST'], 'alltosun.net') !== false) {
                return 'wxsite.alltosun.net';
            }
            return 'demo.syncmeta.com';
        } */

        $subdomain = self::get_subdomain();
        if ($subdomain) {
            return substr($_SERVER['HTTP_HOST'], strlen($subdomain)+1);
        }
        if (strncasecmp('www.', $_SERVER['HTTP_HOST'], 4) == 0) {
            return substr($_SERVER['HTTP_HOST'], 4);
        }
        return $_SERVER['HTTP_HOST'];
    }

    /**
     * 获取当前的subdomain
     * @return string
     */
    public static function get_subdomain()
    {
        $subdomain = '';
        //var_dump($_SERVER['HTTP_HOST']);
        $host     = strtolower($_SERVER['HTTP_HOST']);
        $host_arr = explode('.', $host);
        $count    = count($host_arr);

//        // 开发机伪二级域名
//        if (ONDEV) {
//            if ( $count > 3 ) {
//                $subdomain = $host_arr[0];
//
//                if (in_array($subdomain, config('setting.setting.subdomain_default'))) {
//                    $subdomain = '';
//                }
//            }
//
//            // 正式机
//        } else {
            if ( $count > 3 ) {
                $subdomain = $host_arr[0];
                if (in_array($subdomain, config('setting.setting.subdomain_default'))) {
                    $subdomain = '';
                }
            }
//        }
        //var_dump($host_arr, $subdomain);
        return $subdomain;
    }

    /**
     * 取出是否有setting记录
     * @param int $company_id
     * @return array
     */
    public static function get_setting($field, $open_card_id = 0, $company_id = 0)
    {
        if (!$open_card_id) {
            $open_card_id = OpenCard::get_open_card_id();
        }
        if (!$company_id) {
            $company_id = Company::get_company_id();
        }
        return _uri('setting',array('field'=>$field, 'open_card_id'=>$open_card_id, 'company_id'=>$company_id));
    }


    /**
     * 更新setting信息
     * @param String $field
     * @param String $value
     * @return boolean
     */
    public static function update_setting_info($field, $value, $open_card_id = 0, $company_id = 0)
    {
        if (!$open_card_id) {
            $open_card_id = OpenCard::get_open_card_id();
        }
        if (!$company_id) {
            $company_id = Company::get_company_id();
        }
        // 有，更新
        if (self::get_setting($field, $open_card_id, $company_id)) {
            return Setting::update( array('value'=>$value),array('company_id'=>$company_id, 'open_card_id'=>$open_card_id, 'field'=>$field));

            // 没有，新增一条
        } else {
            return Setting::create(array('company_id'=>$company_id, 'open_card_id'=>$open_card_id, 'field'=>$field, 'value'=>$value));
        }
    }

}